import { useEffect, useState } from "react";
import { getMenus } from "../config/menu";
import { MentuItems } from "../type/layout";
import {useStorage} from "../untils/storeage"
// 核心就是拿到当前menu和全部menu进行diff
export const useLayout = () => {
  const storage = useStorage(window.localStorage)
  const userObj =storage.get("login")
  const userAuthor = userObj !== null ? userObj?.roles : [];
  const [currentMenu, setCurrentMenu] = useState<MentuItems[]>([]);
  useEffect(() => {
    setCurrentMenu(getCurrentMenu());
  }, []);
  const getCurrentMenu = (menuList = getMenus()): MentuItems[] => {
    return menuList.filter((ite: MentuItems) => {
      if (ite.children) {
        ite.children = getCurrentMenu(ite.children);
      }
      return ite.roles.some((ites: string) => userAuthor.includes(ites));
    });
  };
  return [currentMenu];
};
